Ext.define('SRMT.controller.project-explorer.ContextMenu', {
	extend: 'Ext.app.Controller',
	
	views: ['project-explorer.ContextMenu'],
	
	refs: [{
		ref: 'contextMenu',
		selector: 'srmtProjectExplorerContextMenu'
	},{
		ref: 'btnNewProject',
		selector: 'srmtProjectExplorerContextMenu > #btnNewProject'
	},{
		ref: 'btnNewFolder',
		selector: 'srmtProjectExplorerContextMenu > #btnNewFolder'
	},{
		ref: 'btnOpen',
		selector: 'srmtProjectExplorerContextMenu > #btnOpen'
	},{
		ref: 'btnEdit',
		selector: 'srmtProjectExplorerContextMenu > #btnEdit'
	},{
		ref: 'btnDelete',
		selector: 'srmtProjectExplorerContextMenu > #btnDelete'
	},{
		ref: 'btnRefresh',
		selector: 'srmtProjectExplorerContextMenu > #btnRefresh'
	}],
	
	init: function() {
		this.control({
			'srmtProjectExplorerContextMenu > #btnOpen': {
				click: this.onBtnOpenClick
			},
			
			'srmtProjectExplorerContextMenu > #btnNewProject': {
				click: this.onBtnNewProjectClick
			},
			
			'srmtProjectExplorerContextMenu > #btnRefresh': {
				click: this.onBtnRefreshClick
			}
		});
	},
	
	onBtnRefreshClick: function() {
		this.getStore('SRMT.store.ProjectExplorer').load();
	},
	
	onBtnNewProjectClick: function() {
		this.getController('SRMT.controller.Viewport').setViewportDisable();
		var w = Ext.create('Ext.window.Window', {
			title: 'New Project',
			width: 300,
			height: 120,
			resizable: false,
			items: [{
				xtype: 'srmtNewProject'
			}]
		});
		w.show();
		this.getController('SRMT.controller.Viewport').setViewportEnable();
	},
	
	onBtnOpenClick: function() {
		record = this.getContextMenu().record;
		if(record.data.id.substring(0,1) == 'p'){
			var tab = Ext.create('SRMT.view.project-board.ProjectBoard', {title: record.data.text, itemId: record.data.id, closable: true});
			var projectId = parseInt(record.data.id.substr(2, record.data.id.length-2));
			this.getController('SRMT.controller.artifact-list.ArtifactList').getArtifactListByProject(projectId);
		} else if(record.data.id.substring(0,1) == 'f'){
			var tab = Ext.create('SRMT.view.project-board.ProjectBoard', {title: record.parentNode.data.text, itemId: record.parentNode.data.id, closable: true});
			var folderId = parseInt(record.data.id.substr(2, record.data.id.length-2));
			this.getController('SRMT.controller.artifact-list.ArtifactList').getArtifactListByFolder(folderId);
		}
		
		this.getController('SRMT.controller.Viewport').addTabToCenterPanel(tab);
	},
	
	showAt: function(x, y) {
		this.getContextMenu().showAt(x, y);
	},
	
	setContainerContextMenu: function() {
		this.getBtnNewProject().setVisible(true);
		this.getBtnNewFolder().setVisible(false);
		this.getBtnOpen().setVisible(false);
		this.getBtnEdit().setVisible(false);
		this.getBtnDelete().setVisible(false);
		this.getBtnRefresh().setVisible(true);
	},
	
	setProjectContextMenu: function() {
		this.getBtnNewProject().setVisible(false);
		this.getBtnNewFolder().setVisible(true);
		this.getBtnOpen().setVisible(true);
		this.getBtnEdit().setVisible(true);
		this.getBtnDelete().setVisible(true);
		this.getBtnRefresh().setVisible(true);
	},
	
	setFolderContextMenu: function() {
		this.getBtnNewProject().setVisible(false);
		this.getBtnNewFolder().setVisible(false);
		this.getBtnOpen().setVisible(true);
		this.getBtnEdit().setVisible(true);
		this.getBtnDelete().setVisible(true);
		this.getBtnRefresh().setVisible(false);
	}
});